package com.pan.test.mappers;

import com.pan.test.entity.po.Adminuser;
import com.pan.test.entity.po.UserInfo;
import com.pan.test.entity.query.UserInfoQuery;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表 数据库操作接口
 */
public interface UserInfoMapper extends BaseMapper<UserInfo, UserInfoQuery> {

	/**
	 * 根据UserId更新
	 */
	 Integer updateByUserId(@Param("bean") UserInfo userInfo, @Param("userId") String userId);


	/**
	 * 根据UserId删除
	 */
	 Integer deleteByUserId(@Param("userId") String userId);


	/**
	 * 根据UserId获取对象
	 */
	 UserInfo selectByUserId(@Param("userId") String userId);


	/**
	 * 根据email更新
	 */
	Integer updateByemail(@Param("bean") UserInfo userInfo, @Param("email") String email);



	/**
	 * 根据email删除
	 */
	 Integer deleteByemail(@Param("email") String email);


	/**
	 * 根据email获取对象
	 */
	 UserInfo selectByEmail(@Param("email") String email);


	/**
	 * 根据QqOpenId更新
	 */
	 Integer updateByQqOpenId(@Param("bean") UserInfo userInfo, @Param("qqOpenId") String qqOpenId);


	/**
	 * 根据QqOpenId删除
	 */
	 Integer deleteByQqOpenId(@Param("qqOpenId") String qqOpenId);


	/**
	 * 根据QqOpenId获取对象
	 */
	 UserInfo selectByQqOpenId(@Param("qqOpenId") String qqOpenId);


	/**
	 * 根据NickName更新
	 */
	 Integer updateByNickName(@Param("bean") UserInfo userInfo, @Param("nickName") String nickName);


	/**
	 * 根据NickName删除
	 */
	 Integer deleteByNickName(@Param("nickName") String nickName);


	/**
	 * 根据NickName获取对象
	 */
	 UserInfo selectByNickName(@Param("nickName") String nickName);

	Integer updateUserSpace(@Param("userId") String userId, @Param("useSpace") Long useSpace, @Param("totalSpace") Long totalSpace);


	int updateByadminuser(@Param("updataatime") Adminuser updataatime);


	/**
	 * 查询用户注册趋势
	 */
	@Select("WITH RECURSIVE date_sequence AS (" +
			"SELECT a.Date " +
			"FROM (" +
			"   SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date " +
			"   FROM (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a " +
			"   CROSS JOIN (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b " +
			"   CROSS JOIN (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) c " +
			") a " +
			"WHERE a.Date BETWEEN #{startDate} AND #{endDate}" +
			"), " +
			"daily_stats AS (" +
			"   SELECT " +
			"       DATE(join_time) as date, " +
			"       COUNT(*) as count " +
			"   FROM user_info " +
			"   WHERE DATE(join_time) BETWEEN #{startDate} AND #{endDate} " +
			"   GROUP BY DATE(join_time)" +
			") " +
			"SELECT " +
			"   DATE_FORMAT(date_sequence.Date, '%Y-%m-%d') as date, " +
			"   COALESCE(daily_stats.count, 0) as count " +
			"FROM date_sequence " +
			"LEFT JOIN daily_stats ON DATE(date_sequence.Date) = daily_stats.date " +
			"ORDER BY date_sequence.Date ASC")
	List<Map<String, Object>> selectUserTrend(@Param("startDate") String startDate, @Param("endDate") String endDate);

}
